package com.adou.p2p.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.adou.p2p.domain.entiry.Product;
import com.adou.p2p.model.ProductValue;
import com.adou.p2p.page.PageResult;
import org.springframework.data.domain.Page;
import com.adou.p2p.respository.ProductRespository;
import com.adou.p2p.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductRespository productRespository;
	
	@Override
	public ProductValue createProduct(ProductValue value) {
		Product entity = Product.builder()
				.proNum(value.getProNum())
				.proName(value.getProName())
				.proLimit(value.getProLimit())
				.annualized(value.getAnnualized())
				.build();
		
		
		entity = productRespository.save(entity);

		return ProductValue.fromEntity(entity);
	}

	@Override
	public List<ProductValue> findAll() {
		List<ProductValue> productValues = new ArrayList<ProductValue>();
		Iterable<Product> products = productRespository.findAll();
		Iterator<Product> itP= products.iterator();
		while (itP.hasNext()) {
			ProductValue productValue = new ProductValue();
			productValue = ProductValue.fromEntity(itP.next());
			productValues.add(productValue);
		}
		
		return productValues;
	}

	@Override
	public ProductValue findById(Long id) {
		Optional<Product> oProduct = productRespository.findById(id);
		Product product = oProduct.get();
		return ProductValue.fromEntity(product);
	}

	@Override
	public ProductValue updateProduct(ProductValue value) {
		Optional<Product> oEntity = productRespository.findById(value.getId());
		Product entity = oEntity.get();
		
		entity.setAnnualized(value.getAnnualized());
		entity.setProLimit(value.getProLimit());
		entity.setProName(value.getProName());
		entity.setProNum(value.getProNum());
		
	
		entity = productRespository.save(entity);
		return ProductValue.fromEntity(entity);
	}

	@Override
	public PageResult<ProductValue> qryProductValuePage(Integer pageNumber, Integer pageSize, String orderColumn,
			String orderClumnSortString, ProductValue dto) {
		orderColumn = orderColumn == null ? "id" : orderColumn;
		Sort.Direction orderColumnSort = orderClumnSortString == null ? Sort.Direction.DESC
				: Sort.Direction.valueOf(orderClumnSortString);
		Specification<Product> queryConditions = new Specification<Product>() {

			@Override
			public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicates = new ArrayList<>();
				 if(dto.getProName() != null) {
					 predicates.add(criteriaBuilder.like(root.get("proName"), "%" + dto.getProName() + "%"));
				 }
				Predicate predicateAnd = criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
				return query.where(predicateAnd).getRestriction();
			}

		};

		Pageable page = PageRequest.of(pageNumber - 1, pageSize, orderColumnSort, orderColumn);
		Page<Product> products = productRespository.findAll(queryConditions, page);
		List<Product> productList = products.getContent();
		List<ProductValue> productValueList = new ArrayList<>();
		// 返回结果设置
		if (!CollectionUtils.isEmpty(productList)) {
			productList.forEach(t -> {
				ProductValue productValue = new ProductValue();
				BeanCopier.create(Product.class, ProductValue.class, false).copy(t, productValue, null);
				productValueList.add(productValue);
			});
		}
		PageResult<ProductValue> result = new PageResult<>(products.getNumber(), products.getSize(),
				(int) products.getTotalElements(), productValueList);
		return result;

	}

}
